package com.evi.da.question.ask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

@Component
public class ServerConnectorImpl implements ServerConnector
{
	private static final Logger logger = LoggerFactory.getLogger(ServerConnectorImpl.class);


	@Override
	public InputStream getServerConnector(String question)
	{
		InputStream in = null;
		try
		{
			URL da = buildURL(question);
			URLConnection daConnection = da.openConnection();
			in = daConnection.getInputStream();
		} catch (IOException e)
		{
			logger.error("Problem connecting to server", e);
		}
		return in;
	}


	private URL buildURL(String question) throws MalformedURLException, UnsupportedEncodingException
	{
		return new URL("https://api.trueknowledge.com/direct_answer?question="
				+ URLEncoder.encode(question, "UTF-8")
				+ "&object_metadata=image64"
				+ "&question_entities=0"
				+ "&api_account_id=api_tktestrole"
				+ "&api_password=12bek2bldxxqp3iv"
		);
	}
}
